/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;

using Gtk;
using Mono.Unix;

using DCSharp.Xml;

namespace DCSharp.Gui
{
	public class FileListStore : TreeStore
	{
		public enum Column {
			Object,
			Name,
			Bytes,
			Size,
			TTH,
			Loaded
		};

		private FileList fileList;

		public FileListStore(FileList fileList) : base(typeof(object),
			typeof(string), typeof(long), typeof(string), typeof(string),
			typeof(bool))
		{
			FileList = fileList;
		}

		#region Properties

		public FileList FileList
		{
			get { return fileList; }
			set
			{
				if (fileList != null)
				{
					Clear();
				}

				fileList = value;
				if (fileList != null)
				{
					foreach (DirectoryNode directory in fileList.Directories)
					{
						int children = directory.Directories.Count +
							directory.Files.Count;

						string size = String.Format(
							Catalog.GetPluralString("{0} object", "{0} objects",
							children), children);

						TreeIter iter = AppendValues(directory, directory.Name,
							(long)children, size);
						if (children > 0)
						{
							// Add placeholder
							AppendNode(iter);
						}
					}
				}
			}
		}

		#endregion

		#region Methods

		public void Load(TreeIter iter)
		{
			bool loaded = (bool)GetValue(iter, (int)Column.Loaded);
			if (!loaded)
			{
				PopulateIter(iter);

				TreeIter placeholder;
				if (IterChildren(out placeholder, iter))
				{
					Remove(ref placeholder);
				}

				SetValue(iter, (int)Column.Loaded, true);
			}
		}

		private void PopulateIter(TreeIter parent)
		{
			DirectoryNode directory = GetValue(parent, (int)Column.Object)
				as DirectoryNode;
			if (directory != null)
			{
				string size;
				foreach (DirectoryNode childDir in directory.Directories)
				{
					int children = childDir.Directories.Count +
						childDir.Files.Count;

					size = String.Format(Catalog.GetPluralString("{0} object", "{0} objects",
						children), children);

					TreeIter iter = AppendValues(parent, childDir, childDir.Name,
						(long)children, size);
					if (children > 0)
					{
						// Add placeholder
						AppendNode(iter);
					}
				}
				foreach (FileNode file in directory.Files)
				{
					size = Util.FormatFileSize(file.Size);
					AppendValues(parent, file, file.Name, file.Size, size,
						file.TTH);
				}
			}
		}

		#endregion
	}
}
